import LoginStore from '../../pages/login/store.js';
import { proxy, reqHeader, isAuthorization, followAction } from '../../utils/util.js';
import contestUtil from '../../utils/contestUtil.js';
let getDefaultData = function () {
  return {
    listData: [],
    hasNext: true
  }
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    urlStr: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {

      }
    },
    resourceType: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
          
      }
    },
    params: {
      type: Object,
      value: {},
      observer: function (newVal, oldVal, changedPath) {
        if (newVal.page == 1) {
          this.setData(getDefaultData())
        }
        if (newVal.page && this.data.hasNext) {
          this._loadData(newVal);
        }
      }
    },
    totalPageUrl: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {

      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: getDefaultData(),

  /**
   * 组件的方法列表
   */
  methods: {
    toUserDetail:function(e){
      let userId = e.currentTarget.dataset.userid
      wx.navigateTo({
        url: '/pages/userInfo/userInfo?userId=' + userId
      });
    },
    toAlbumDetail: function (e) {
      let urlStr = `/pages/album/album?galleryId=${e.currentTarget.dataset.galleryid}`;
      let pageList = getCurrentPages();
      if (pageList.length >= 8) {
        wx.redirectTo({
          url: urlStr,
        })
      } else {
        wx.navigateTo({
          url: urlStr
        });
      }
    },
    stoppropagation: function () {
      console.log('stoppropagation');
    },
    submitWorks: function (e) {
      if (!isAuthorization(e)) {
        return;
      }
      let contestId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/contest/contest?id=${contestId}&showTheme=true`
      });
    },
    clickCard: function (e) {
      let contestId = e.currentTarget.dataset.id;
      wx.request({
        url: `${proxy}/alilog/put`,
        data: {
          objId: 'enter-quest-mini-program-list-card',
          contestId: contestId
        },
        method:'POST',
        header: Object.assign({
          'content-type': 'application/x-www-form-urlencoded'
        }, reqHeader),
        success: function (res) {
          console.log(res);
        }
      });
      wx.navigateTo({
        url: `/pages/contest/contest?id=${contestId}`
      });
    },
    _loadData: function (params) {
      let me = this;
      let userAccountInfo = LoginStore.userInfo || {};
      params['access_token'] = userAccountInfo.access_token;
      wx.request({
        url: this.properties.urlStr,
        data: params,
        header: reqHeader,
        success: function (res) {
          res = res.data;
          if (res.status == 200) {
            let hasNext = true;
            if (!res.data.length || res.data.length < (params.size || 20)) {
              hasNext = false;
            }
            if (me.properties.resourceType === "contest"){
              for (let item of res.data) {
                item.timeStr = contestUtil.getContestRTime(item, true);
              }
            }
            let listData = me.data.listData.concat(res.data);
            me.setData({
              listData: listData,
              hasNext: hasNext
            })
          } else {
            console.log(res.message);
          }
        }
      });
    },
    followAction: function (e) {
      let me = this;
      let idx = e.currentTarget.dataset.idx;
      let listData = me.data.listData;
      let userInfo = listData[idx];
      let callback = () => {
        me.triggerEvent('tabclick', {name: "user"});
      }
      let resback = () => {
        me.setData({
          listData: listData
        })
      }
      followAction(e, me, userInfo, "cardList", callback, resback);
    },
    selectAlbun(e) {
      let album = e.currentTarget.dataset.album;

      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.addGallery(album, function () {
        wx.navigateBack({
          delta: 1
        })
      })
    }
  }
})
